package com.paddyi.controller;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.paddyi.commons.qiniu.MyQiniu;
import com.paddyi.commons.utils.DateUtil;
import com.paddyi.commons.utils.MD5;
import com.paddyi.commons.utils.StringUtils;
import com.paddyi.model.Role;
import com.paddyi.model.UserRole;
import com.paddyi.model.vo.UserVo;
import com.paddyi.service.IResourceService;
import com.paddyi.service.IUserRoleService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.paddyi.commons.http.HttpResult;
import com.paddyi.commons.page.MyPage;
import com.paddyi.model.User;
import com.paddyi.service.IUserService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户 (User)
 * @author paddy
 * @since  2019-05-10 02:53:26
 */
@RestController
@RequestMapping("user")
public class UserController extends BaseController {

	@Autowired
	private IUserService userService;
	@Autowired
	private IResourceService resourceService;
	@Autowired
	private IUserRoleService userRoleService;

	/**
	 * 添加用户
	 * @param file
	 * @return
	 */
	@PostMapping(value="/addImg")
	public HttpResult addImg(MultipartFile file) {
		String date = new Date().getTime()+"";
		String fileName = file.getOriginalFilename();
		String[] fileNames = fileName.split("\\.");
		String type = fileNames[1];
		String name = date+"."+type;
		MyQiniu.uploadMultipartFile(file,name,true);
		return HttpResult.ok("http://qiniu.paddyi.com/"+name+"?imageView2/1/w/200/h/200/q/75|imageslim");
	}

	/**
	 * 添加用户
	 * @param record
	 * @return
	 */	
	@PostMapping(value="/add")
	public HttpResult add(@RequestBody UserVo record) {
		String password = MD5.getMd5Str(record.getPassword());
		record.setPassword(password);
		record.setLoginName(record.getName());
		record.setCreateTime(new Date());
		return HttpResult.ok(userService.save(record));
	}

	/**
	 * 修改用户
	 * @param record
	 * @return
	 */
	@PostMapping(value="/edit")
	public HttpResult edit(@RequestBody UserVo record) {
		if(StringUtils.isNotBlank(record.getPassword())){
			record.setPassword(MD5.getMd5Str(record.getPassword()));
		}
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("user_id",record.getId());
		userRoleService.remove(queryWrapper);
		if(record.getRoleList().size() > 0){
			for(Role r:record.getRoleList()){
				UserRole userRole = new UserRole();
				userRole.setRoleId(r.getId());
				userRole.setUserId(record.getId());
				userRoleService.save(userRole);
			}

		}
		return HttpResult.ok(userService.updateById(record));
	}

    /**
     * 删除用户
     * @param records
     * @return
     */
	@PostMapping(value="/delete")
	public HttpResult delete(@RequestBody List<User> records) {
		List list = new ArrayList();
		for(User or : records){
			list.add(or.getId());
		}
		return HttpResult.ok(userService.removeByIds(list));
	}

    /**
     * 基础分页查询,条件查询只支持等于，如果想要更高级的功能，比如大于小于等等，请自己写吧
     * @param page
     * @return
     */    
	@PostMapping(value="/findPage")
	public HttpResult findPage(@RequestBody MyPage<User> page) {
		Page<UserVo> rePage = userService.selectUserVoPage(page);
		return HttpResult.ok(rePage);
	}
	
    /**
     * 根据主键查询
     * @param id
     * @return
     */ 	
	@GetMapping(value="/findById")
	public HttpResult findById(@RequestParam Long id) {
		return HttpResult.ok(userService.selectUserVoById(id));
	}

	/**
	 * 查询权限
	 * @return
	 */
	@GetMapping(value="/findPermissions")
	public HttpResult findPermissions() {
		UserVo user = (UserVo) SecurityUtils.getSubject().getPrincipal();
		List<Long> roleIds = new ArrayList<>();
		for(Role role : user.getRoleList()){
			roleIds.add(role.getId());
		}
		return HttpResult.ok(resourceService.selectResourceButtonByLoginName(roleIds));
	}
}
